package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.utils.AppContext;
import com.ms.common.utils.MsDateUtils;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.account.Withdraw;
import com.ms.p2p.entity.member.Auth;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.provider.CertiFiCationProvider;
import com.ms.p2p.provider.MemberAccountProvider;
import com.ms.p2p.provider.MemberAccountRecordProvider;
import com.ms.p2p.provider.MemberWithDrawProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 提现管理
 */
@Controller("AdminCashController")
@RequestMapping({ "/admin/cash" })
public class AdminCashController extends AdminBaseController {

	@Reference
	private MemberWithDrawProvider memberWithDrawProvider;
	@Reference
	private MemberAccountProvider memberAccountProvider;
	@Reference
	private MemberAccountRecordProvider accountLogService;
	@Reference
	private CertiFiCationProvider certificationProvider;


	@RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
	@ResponseBody
	public PageBean queryData(HttpServletRequest request, HttpSession session
			,@RequestParam(value = "status",required = false) String status
			,@RequestParam(value = "userId",required = false) String userId
			,@RequestParam(value = "userName",required = false) String userName
			,@RequestParam(value = "end_date",required = false) String end_date
			,@RequestParam(value = "begin_date",required = false) String begin_date,
							  PageBean page
	) {

		page.setList(memberWithDrawProvider.findPager(status, userId,
				userName, begin_date, end_date, page));
		page.setTotalCount(memberWithDrawProvider.findCount(status, userId, userName,
				begin_date, end_date));
		return JsonDataGridHelper.createJSONData(page);
	}

	/**
	 * 后台查看提现控制器
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping({ "/list" })
	public String list(HttpServletRequest request, HttpSession session

	) {
		return "admin/account/record/cash/list";
	}

	/**
	 * 提现信息的详情 TODO 增加TOKEN防止重复提交？？？
	 */
	@RequestMapping({ "/{cashId}" })
	public String viewReview(@PathVariable String cashId, Model model) {

		if (StringUtils.isNotBlank(cashId)) {
			Map<String, Object> cash = memberWithDrawProvider.getEmplyoee(Integer
					.parseInt(cashId));
			model.addAttribute("cash", cash);
			int userId = (Integer) cash.get("user_id");
			Auth auth = certificationProvider.findByUserId(userId);
			model.addAttribute("realName", auth);
			model.addAttribute("logs",accountLogService.findPager(userId+"",null,null,null,null,0,20));
		}
		return "admin/account/record/cash/input";
	}

	@RequestMapping("/exportCashList")
	public String exportCashList(HttpServletRequest request, Model model
			,@RequestParam(value = "userId",required = false) String userId
			,@RequestParam(value = "userName",required = false) String userName
			,@RequestParam(value = "begin_date",required = false) String begin_date
			,@RequestParam(value = "end_date",required = false) String end_date

	) {
		List<Map<String, Object>> cashs = memberWithDrawProvider.findList(
				request.getParameter("status"), userId, userName, begin_date,
				end_date);
		for (Map<String, Object> map : cashs) {
			String real_name = (String) map.get("real_name");
			if (StringUtils.isEmpty(real_name)) {
				map.put("real_name", (String) map.get("pany_name"));
			}
			Object total = map.get("total");
			if (total != null) {
				map.put("total", total.toString());
			}


			String addtime = (String) map.get("addtime");
			if (addtime != null) {
				map.put("addtime", MsDateUtils.TimeStamp2DateTime(addtime));
			}
			Integer status = (Integer) map.get("status");
			if (status != null) {
				if (status == 0) {
					map.put("status", "申请中");
				} else if (status == 1) {
					map.put("status", "审核通过");
				} else {
					map.put("status", "审核未通过");
				}
			}
		}
		ExcelData excelData = new ExcelData();
		excelData.setHeads(new String[] { "用户ID", "用户名称", "真实姓名", "提现编号",
				"提现账号", "提现银行", "支行", "提现总额",  "申请时间", "状态" });
		excelData.setFieldNames(new String[] { "user_id", "u_user_name",
				"real_name", "id", "crad_no", "bank", "branch", "total",
				"addtime", "status" });
		excelData.setDataList(cashs);
		excelData.setModelClass(Map.class);
		model.addAttribute("excelData", excelData);
		model.addAttribute("fileName", "提现申请");
		return "excel";
	}


	/**
	 * 提现审核  TODO TOKEN验证防止重复审核？？？？
	 * @return
	 */
	@RequestMapping("/cashVerify")
	@ResponseBody
	public synchronized Message cashVerify(String cashId, String remark, String status) throws IllegalAccessException,
			InvocationTargetException, IntrospectionException {
		AdminUser emp = getCurrentEmp();
		Withdraw withdraw = memberWithDrawProvider.findByid(cashId);
		withdraw.setStatus(Integer.parseInt(status));
		withdraw.setVerify_userid(emp.getId());
		withdraw.setVerify_remark(remark);
		withdraw.setVerify_time(MsDateUtils.getTime());
		memberAccountProvider.cashVerify( AppContext.getRemoteIp(),withdraw, remark);
		return Message.success("审核成功！");
	}

	@Override
	public boolean getBinderFlag() {
		return true;
	}
}
